<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>把数组中的值赋给一组变量</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.ksort.html">« ksort</a></li>
      <li style="float: right;"><a href="function.natcasesort.html">natcasesort »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>把数组中的值赋给一组变量</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.list" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">list</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">list</span> &mdash; <span class="dc-title">把数组中的值赋给一组变量 </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.list-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>list</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$vars</code><span class="initializer"> = ?</span></span>): <span class="type">array</span></div>

    <p class="para rdfs-comment">
     像 <span class="function"><a href="function.array.html" class="function">array()</a></span> 一样，这不是真正的函数，而是语言结构。
     <span class="function"><strong>list()</strong></span> 可以在单次操作内为一组变量赋值。字符串不能解包且
     <span class="function"><strong>list()</strong></span> 表达式不能完全为空。
    </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 PHP 7.1.0 之前的版本，<span class="function"><strong>list()</strong></span> 仅能用于数字索引的数组，并假定数字索引从 0 开始。
   </p>
  </p></blockquote>
   </div>

   <div class="refsect1 parameters" id="refsect1-function.list-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">var</code></dt>

     <dd>

      <p class="para">
       一个变量。
      </p>
     </dd>

    
   </dl>

   <dl>
    
     <dt>
<code class="parameter">vars</code></dt>

     <dd>

      <p class="para">
       更多变量。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.list-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回指定的数组。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.list-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.3.0</td>
       <td>
        支持在数组解构时传引用。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        现在可以指定 <span class="function"><strong>list()</strong></span> 中的键。
        这就可以解构非数字键或者无顺序的数组。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.list-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4996">
    <p><strong>示例 #1 <span class="function"><strong>list()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$info&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'coffee'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'brown'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'caffeine'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;列出所有变量<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$drink</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$drink</span><span style="color: #DD0000">&nbsp;is&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #DD0000">&nbsp;and&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #DD0000">&nbsp;makes&nbsp;it&nbsp;special.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;列出他们的其中一个<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$drink</span><span style="color: #007700">,&nbsp;,&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$drink</span><span style="color: #DD0000">&nbsp;has&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;或者让我们跳到仅第三个<br /></span><span style="color: #007700">list(&nbsp;,&nbsp;,&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$info</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;need&nbsp;</span><span style="color: #0000BB">$power</span><span style="color: #DD0000">!\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;list()&nbsp;不能对字符串起作用<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #DD0000">"abcde"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;NULL<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4997">
    <p><strong>示例 #2 <span class="function"><strong>list()</strong></span> 用法的一个例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$pdo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;id,&nbsp;name&nbsp;FROM&nbsp;employees"</span><span style="color: #007700">);<br />while&nbsp;(list(</span><span style="color: #0000BB">$id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_NUM</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"id:&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">,&nbsp;name:&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4998">
    <p><strong>示例 #3 使用嵌套的  <span class="function"><strong>list()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;list(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">))&nbsp;=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents screen">
<div class="cdata"><pre>
int(1)
int(2)
int(3)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4999">
    <p><strong>示例 #4 <span class="function"><strong>list()</strong></span> 和索引顺序定义</strong></p>
    <div class="example-contents"><p>
     <span class="function"><strong>list()</strong></span> 使用 array 索引的顺序和它何时定义无关。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'c'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'d'</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$z</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$z</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     得到以下输出（注意比较 <span class="function"><strong>list()</strong></span> 所写的元素顺序）：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(4) {
  [2]=&gt;
  string(1) &quot;a&quot;
  [&quot;foo&quot;]=&gt;
  string(1) &quot;b&quot;
  [0]=&gt;
  string(1) &quot;c&quot;
  [1]=&gt;
  string(1) &quot;d&quot;
}
string(1) &quot;c&quot;
string(1) &quot;d&quot;
string(1) &quot;a&quot;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5000">
    <p><strong>示例 #5 带键的 <span class="function"><strong>list()</strong></span></strong></p>
    <div class="example-contents"><p>
     从 PHP 7.1.0 开始，<span class="function"><strong>list()</strong></span> 可以包含显式的键，可赋值到任意表达式。
     可以混合使用数字和字符串键。但是不能混合有键和无键不能混用。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$data&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">"id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Tom'</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">"id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Fred'</span><span style="color: #007700">],<br />];<br />foreach&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">as&nbsp;[</span><span style="color: #DD0000">"id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"id:&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">,&nbsp;name:&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$second</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$fourth</span><span style="color: #007700">)&nbsp;=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$second</span><span style="color: #DD0000">,&nbsp;</span><span style="color: #0000BB">$fourth</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
id: 1, name: Tom
id: 2, name: Fred

2, 4
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.list-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.each.html" class="function" rel="rdfs-seeAlso">each()</a> - 返回数组中当前的键／值对并将数组指针向前移动一步</span></li>
    <li class="member"><span class="function"><a href="function.array.html" class="function" rel="rdfs-seeAlso">array()</a> - 新建一个数组</span></li>
    <li class="member"><span class="function"><a href="function.extract.html" class="function" rel="rdfs-seeAlso">extract()</a> - 从数组中将变量导入到当前的符号表</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>